package com.sltx.util;

import java.io.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tools {

	public static DecimalFormat dFormat1 = new DecimalFormat("#,##0.00");
	public static DecimalFormat dFormat2 = new DecimalFormat("#,##0");

	/**
	 * 随机生成六位数验证码
	 * 
	 * @return
	 */
	public static int getRandomNum() {
		Random r = new Random();
		return r.nextInt(900000) + 100000;// (Math.random()*(999999-100000)+100000)
	}

	/**
	 * 检测字符串是否不为空(null,"","null")
	 * 
	 * @param s
	 * @return 不为空则返回true，否则返回false
	 */
	public static boolean notEmpty(String s) {
		return s != null && !"".equals(s) && !"null".equals(s);
	}

	/**
	 * 检测字符串是否为空(null,"","null")
	 * 
	 * @param s
	 * @return 为空则返回true，不否则返回false
	 */
	public static boolean isEmpty(String s) {
		return s == null || "".equals(s) || "null".equals(s);
	}

	/**
	 * 字符串转换为字符串数组
	 * 
	 * @param str
	 *            字符串
	 * @param splitRegex
	 *            分隔符
	 * @return
	 */
	public static String[] str2StrArray(String str, String splitRegex) {
		if (isEmpty(str)) {
			return null;
		}
		return str.split(splitRegex);
	}

	/**
	 * 用默认的分隔符(,)将字符串转换为字符串数组
	 * 
	 * @param str
	 *            字符串
	 * @return
	 */
	public static String[] str2StrArray(String str) {
		return str2StrArray(str, ",\\s*");
	}

	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，日期转字符串
	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String date2Str(Date date) {
		return date2Str(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，字符串转日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date str2Date(String date) {
		if (notEmpty(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return new Date();
		} else {
			return null;
		}
	}

	/**
	 * 按照参数format的格式，日期转字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date, String format) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		} else {
			return "";
		}
	}

	/**
	 * 把时间根据时、分、秒转换为时间段
	 * 
	 * @param StrDate
	 */
	public static String getTimes(String StrDate) {
		String resultTimes = "";

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now;

		try {
			now = new Date();
			Date date = df.parse(StrDate);
			long times = now.getTime() - date.getTime();
			long day = times / (24 * 60 * 60 * 1000);
			long hour = (times / (60 * 60 * 1000) - day * 24);
			long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

			StringBuffer sb = new StringBuffer();
			// sb.append("发表于：");
			if (hour > 0) {
				sb.append(hour + "小时前");
			} else if (min > 0) {
				sb.append(min + "分钟前");
			} else {
				sb.append(sec + "秒前");
			}

			resultTimes = sb.toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return resultTimes;
	}

	/**
	 * 写txt里的单行内容
	 * 
	 * @param filePath
	 *            文件路径
	 * @param content
	 *            写入的内容
	 */
	public static void writeFile(String fileP, String content) {
		String filePath = String.valueOf(Thread.currentThread()
				.getContextClassLoader().getResource(""))
				+ "../../"; // 项目路径
		filePath = (filePath.trim() + fileP.trim()).substring(6).trim();
		if (filePath.indexOf(":") != 1) {
			filePath = File.separator + filePath;
		}
		try {
			OutputStreamWriter write = new OutputStreamWriter(
					new FileOutputStream(filePath), "utf-8");
			BufferedWriter writer = new BufferedWriter(write);
			writer.write(content);
			writer.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 验证邮箱
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 验证手机号码
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean checkMobileNumber(String mobileNumber) {
		boolean flag = false;
		try {
			Pattern regex = Pattern
					.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
			Matcher matcher = regex.matcher(mobileNumber);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 检测KEY是否正确
	 * 
	 * @param paraname
	 *            传入参数
	 * @param FKEY
	 *            接收的 KEY
	 * @return 为空则返回true，不否则返回false
	 */
	public static boolean checkKey(String paraname, String FKEY) {
		paraname = (null == paraname) ? "" : paraname;
		return MD5.md5(paraname + DateUtil.getDays() + ",fh,").equals(FKEY);
	}

	/**
	 * 读取txt里的单行内容
	 * 
	 * @param filePath
	 *            文件路径
	 */
	public static String readTxtFile(String fileP) {
		try {

			String filePath = String.valueOf(Thread.currentThread()
					.getContextClassLoader().getResource(""))
					+ "../../"; // 项目路径
			filePath = filePath.replaceAll("file:/", "");
			filePath = filePath.replaceAll("%20", " ");
			filePath = filePath.trim() + fileP.trim();
			if (filePath.indexOf(":") != 1) {
				filePath = File.separator + filePath;
			}
			String encoding = "utf-8";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding); // 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					return lineTxt;
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件,查看此路径是否正确:" + filePath);
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
		}
		return "";
	}

	/**
	 * 根据权限普通用户和高级用户转换值
	 * 
	 * @param value
	 * @return
	 * @throw
	 * @return String
	 */
	public static String translateValue(String value, int type, int formattype,int formatFlag) {
		try {
			if (!"".equals(value) && value != null) {
				// value = dFormat1.format(b) + "-" + dFormat1.format(c);
				double value_str = Double.parseDouble(value);
				if (formattype == 0) {
					if (type == 0) {
//						if (0 <= value_str && value_str < 2000) {
//							value = "0-2000";
//						} else if (2000 <= value_str && value_str < 10000) {
//							value = "2000-10000";
//						} else if (10000 <= value_str && value_str < 100000) {
//							value = "10000-100000";
//						} else if (100000 <= value_str) {
//							value = "100000以上";
//						}
						if (value_str < 0) {
							value = "0以下";
						} else if (0 <= value_str && value_str < 100) {
							value = "0-100";
						} else if (100 <= value_str && value_str < 500) {
							value = "100-500";
						} else if (500 <= value_str && value_str < 1000) {
							value = "500-1000";
						} else if (1000 <= value_str && value_str < 5000) {
							value = "1000-5000";
						} else if (5000 <= value_str && value_str < 10000) {
							value = "5000-10000";
						} else if (10000 <= value_str && value_str < 100000) {
							value = "10000-100000";
						} else if (100000 <= value_str && value_str < 1000000) {
							value = "100000-1000000";
						} else if (1000000 <= value_str) {
							value = "1000000以上";
						}
					} else if (type == 1){
						if (0 <= value_str && value_str < 100) {
							value = "0-100";
						} else if (100 <= value_str && value_str < 1000) {
							value = "100-1000";
						} else if (1000 <= value_str && value_str < 10000) {
							value = "1000-10000";
						} else if (10000 <= value_str && value_str < 100000) {
							value = "10000-100000";
						} else if (100000 <= value_str) {
							value = "100000以上";
						}
					} else{
						if (0 <= value_str && value_str < 10) {
							value = "0-10";
						} else if (10 <= value_str && value_str < 50) {
							value = "10-50";
						} else if (50 <= value_str && value_str < 100) {
							value = "50-100";
						} else if (100 <= value_str && value_str < 200) {
							value = "100-200";
						} else if (200 <= value_str && value_str < 500) {
							value = "200-500";
						} else if (500 <= value_str && value_str < 1000) {
							value = "500-1000";
						} else if (1000 <= value_str && value_str < 5000) {
							value = "1000-5000";
						} else if (5000 <= value_str && value_str < 10000) {
							value = "5000-10000";
						} else if (10000 <= value_str) {
							value = "10000以上";
						}
					}
				} else {
					if(formatFlag == 0){
						value = dFormat1.format(value_str);
					}else{
						value = dFormat2.format(value_str);
					}
				}
			} else {
				if(formatFlag == 0){
					value = "0.00";
				}else{
					value = "0";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 企业列表的权限
	 * 
	 * @param list
	 * @return
	 * @throw
	 * @return List<PageData>
	 */
	public static List<PageData> translateDataQYLB(List<PageData> list,
			int formattype) {
		List<PageData> yqlblist = new ArrayList<PageData>();
		try {
			if (list != null && list.size() > 0) {
				int size = list.size();
				PageData pageData = null;
				for (int i = 0; i < size; i++) {
					pageData = list.get(i);
					//销售收入
					if(pageData.get("CAPTI") != null && !pageData.get("CAPTI").toString().equals("")){
						pageData.put("CAPTI", translateValue(pageData.get("CAPTI").toString(), 0, formattype,0));
					}
					//投资金额
					if(pageData.get("CZNUM") != null && !pageData.get("CZNUM").toString().equals("")){
						pageData.put("CZNUM", translateValue(pageData.get("CZNUM").toString(), 1, formattype,0));
					}
					//从业人数
					if(pageData.get("CYNUM") != null && !pageData.get("CYNUM").toString().equals("")){
						pageData.put("CYNUM", translateValue(pageData.get("CYNUM").toString(), 2, formattype,1));
					}
					//投资金额
					if(pageData.get("INV_AMOUNT") != null && !pageData.get("INV_AMOUNT").toString().equals("")){
						pageData.put("INV_AMOUNT", translateValue(pageData.get("INV_AMOUNT").toString(), 1, formattype,0));
					}
					//独资企业投资金额
					if(pageData.get("COUNT1") != null && !pageData.get("COUNT1").toString().equals("")){
						pageData.put("COUNT1", translateValue(pageData.get("COUNT1").toString(), 1, formattype,0));
					}
					//控股企业投资金额
					if(pageData.get("COUNT2") != null && !pageData.get("COUNT2").toString().equals("")){
						pageData.put("COUNT2", translateValue(pageData.get("COUNT2").toString(), 1, formattype,0));
					}
					//参股企业投资金额
					if(pageData.get("COUNT3") != null && !pageData.get("COUNT3").toString().equals("")){
						pageData.put("COUNT3", translateValue(pageData.get("COUNT3").toString(), 1, formattype,0));
					}
					//利润
					if(pageData.get("ENT_PROFIT") != null && !pageData.get("ENT_PROFIT").toString().equals("")){
						pageData.put("ENT_PROFIT", translateValue(pageData.get("ENT_PROFIT").toString(), 0, formattype,0));
					}
					//纳税
					if(pageData.get("ENT_TAX") != null && !pageData.get("ENT_TAX").toString().equals("")){
						pageData.put("ENT_TAX", translateValue(pageData.get("ENT_TAX").toString(), 0, formattype,0));
					}
					yqlblist.add(pageData);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return yqlblist;
	}

	public static void main(String[] args) {
		System.out.println(getRandomNum());
	}

}
